package com.zhku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.AttendanceRecord;
import com.zhku.meeting.entities.Department;
import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.entities.Staff;
import com.zhku.meeting.mapper.AttendanceRecordMapper;
import com.zhku.meeting.mapper.DepartmentMapper;
import com.zhku.meeting.mapper.StaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private AttendanceRecordMapper attendanceRecordMapper;

    //添加的查重
    public  Msg duplicateCheck1(Map<String, Object> para) {
        String name = (String) para.get("name");
        String email = (String) para.get("email");
        Staff staff = new Staff(null,name,email,null,null,null);
        if (para.containsKey("staffId")){
            staff.setStaffId((Integer) para.get("staffId"));
        }
        List<Staff> staffList = staffMapper.duplicateCheck1(staff);
        if (staffList.size()==0){
            return Msg.success();
        }else{
            String error = "";
            Boolean flag = false;
            if (name.equals(staffList.get(0).getName())){
                error = "员工姓名重复";
                flag = true;
            }
            if (email.equals(staffList.get(0).getEmail())){
                error = flag?"以及email重复":"email重复";
            }
            return Msg.fault().setError(error);
        }
    }
    //修改的查重
    public  Msg duplicateCheck(Map<String, Object> para) {
        String name = (String) para.get("name");
        String email = (String) para.get("email");
        Integer staffId = (Integer) para.get("staffId");
        Staff staff = new Staff(staffId,name,email,null,null,null);
        Staff staff1 = staffMapper.selectByPrimaryKey(staffId);
        if(name.equals(staff1.getName())&&email.equals(staff1.getEmail())){
            return Msg.success();
        }
        List<Staff> staffList = staffMapper.duplicateCheck(staff);
        if (staffList.size()==0){
            return Msg.success();
        }else{
            String error = "";
            Boolean flag = false;
            if (name.equals(staffList.get(0).getName())&&email.equals(staffList.get(0).getEmail())){
                error = "重复";
                return Msg.fault().setError(error);
            }
            return Msg.success();
        }
    }

    public Msg getStaff(Map<String,Object> para){
        Integer id = Integer.parseInt((String) para.get("staffId"));
        Staff staff = staffMapper.selectByPrimaryKey(id);
        Map<String,Object> map = new HashMap<>();
        map.put("result",staff);
        return staff!=null?Msg.success().add(map):Msg.fault();
    }

    public Msg getAll(Map<String, Object> para) {
        List<Staff> staffList;
        Map<String,Object> map = new HashMap<>();
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            staffList = staffMapper.selectAll();
            PageInfo pageInfo = new PageInfo(staffList);
            map.put("result",pageInfo);
        }
        else {
            staffList = staffMapper.selectAll();
            map.put("result",staffList);
        }
        return Msg.success().add(map);
    }

    public Msg addStaff(Map<String, Object> para) {
        String name = (String) para.get("name");
        String email = (String) para.get("email");
        String deptName = (String) para.get("deptName");
        Staff staff = new Staff(null,name,email,"123456",deptName,0);
        int num = staffMapper.insert(staff);
        if (num>0){
            Integer deptNum = staffMapper.selectDeptNum(deptName);
            Department department = new Department(null,deptName,null,deptNum);
            departmentMapper.updateDeptNum(department);
        }
        return num>0?Msg.success():Msg.fault();
    }

    /**
     * 根据Id删除员工，并更新员工所在部门的人数
     * @param para
     * @return
     */
    public Msg deleteById(@RequestBody Map<String, Object> para) {
        Integer staffId = (Integer) para.get("staffId");
        Staff staff = new Staff(staffId,null,null,null,null,null);
        String deptName = staffMapper.selectDeptByStaffId(staff);
        int num = staffMapper.deleteByPrimaryKey(staffId);
        if (num>0){
            Integer deptNum = staffMapper.selectDeptNum(deptName);
            Department department = new Department(null,deptName,null,deptNum);
            departmentMapper.updateDeptNum(department);
        }
        return num>0?Msg.success():Msg.fault();
    }

    public Msg updateStaff(Map<String, Object> para) {
        Integer staffId =  (Integer)para.get("staffId");
        String name = null;
        String email = null;
        String password = null;
        String deptName = null;
//        Integer staffState = null;
        if (para.containsKey("name")){
            name = (String) para.get("name");
        }
        if (para.containsKey("email")){
            email = (String) para.get("email");
        }
        if (para.containsKey("password")){
            password = (String) para.get("password");
        }
        if (para.containsKey("deptName")){
            deptName = (String) para.get("deptName");
        }

        Staff staff = new Staff(staffId,name,email,password,deptName,null);
        String deptName1 = staffMapper.selectDeptByStaffId(staff);
        int num = staffMapper.updateByPrimaryKey(staff);

        if (deptName != null && num > 0){
            Integer deptNum1 = staffMapper.selectDeptNum(deptName1);
            Department department1 = new Department(null,deptName1,null,deptNum1);
            departmentMapper.updateDeptNum(department1);
            Integer deptNum = staffMapper.selectDeptNum(deptName);
            Department department = new Department(null,deptName,null,deptNum);
            departmentMapper.updateDeptNum(department);
        }
        return num>0?Msg.success():Msg.fault();
    }

    public Msg selectCondition(Map<String, Object> para) {
        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
        Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
        String name = null;
        String email = null;
//        String password = null;
        String deptName = null;
        if (para.containsKey("name")){
            name = "%"+para.get("name")+"%";
        }
        if (para.containsKey("email")){
            email = "%"+para.get("email")+"%";
        }
//        if (para.containsKey("password")){
//            password = "%"+para.get("password")+"%";
//        }
        if (para.containsKey("deptName")){
            deptName = "%"+para.get("deptName")+"%";
        }
        Staff staff = new Staff(null,name,email,null,deptName,null);
        PageHelper.startPage(currentPage,pageSize);
        List<Staff> staffList = staffMapper.selectByCondition(staff);
        PageInfo pageInfo = new PageInfo(staffList);
        Map<String,Object> map = new HashMap<>();
        map.put("result",pageInfo);
        return staffList.size()!=0?Msg.success().add(map):Msg.fault();
    }


    public String selectByUsername(String username) {
        return staffMapper.selectByEmail(username);
    }


    public Msg userUpdateStaff(Map<String, Object> para) {
        Integer staffId = (Integer) para.get("staffId");
        String name = null;
        String email = null;
        String password = null;
        String deptName = null;
        Integer staffState=null;
//        Integer staffState = null;
        if (para.containsKey("name")){
            name = (String) para.get("name");
        }
        if (para.containsKey("email")){
            email = (String) para.get("email");
        }
        if (para.containsKey("password")){
            password = (String) para.get("password");
        }
        if (para.containsKey("deptName")){
            deptName = (String) para.get("deptName");
        }
        if (para.containsKey("staffState")){
            staffState = (Integer) para.get("staffState");
        }

        Staff staff = new Staff(staffId,name,email,password,deptName,staffState);
        int num = staffMapper.userUpdateStaff(staff);
        if (deptName != null && num > 0){
            Integer deptNum = staffMapper.selectDeptNum(deptName);
            Department department = new Department(null,deptName,null,deptNum);
            departmentMapper.updateDeptNum(department);
        }
        return num>0?Msg.success():Msg.fault();
    }
    public String getDeptNameById(String staffId) {
        return staffMapper.selectDeptByStaffId(staffId);
    }

    /**
     * 添加参会人员
     * @param attendance
     * @param para
     * @return
     */
    public boolean setAttendance(String attendance,Map<String ,Object>para) {
        AttendanceRecord attendanceRecord=new AttendanceRecord();
        attendanceRecord.setRecordId((Integer) para.get("recordId"));
        attendanceRecord.setAttenState(1);
        attendanceRecord.setStaffName(attendance);
        int result = attendanceRecordMapper.insert(attendanceRecord);
        if (result<=0){
            return false;
        }else {
            return true;
        }
    }

    public Msg resetPassword(Map<String, Object> para) {
        Integer staffId =  Integer.parseInt((String) para.get("staffId"));
        Staff staff = new Staff(staffId,null,null,"123456",null,null);
        int num = staffMapper.updatePassword(staff);
        return num>0?Msg.success():Msg.fault();
    }
}
